package koscom.corebase.nam;

import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.Locale;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.text.MaskFormatter;
import javax.swing.text.NumberFormatter;

import org.jdesktop.swingx.JXDatePicker;


@SuppressWarnings("serial")
public class A2003 extends JFrame {
	
	private JPanel contentPane;
	private JFormattedTextField txtAccNum;
	private JTextField txtAccName;
	private JFormattedTextField txtJKAmount;
	private JTextField txtJKName;
	private JTextField txtJKSSN;
	private JTextField txtJKPhone1;
	private JTextField txtJKAddress;
	private JTextField txtJKetc;
	private JTextField txtJKPhone2;
	private JTextField txtJKPhone3;
	private JPasswordField txtPassword;
	private JTextField txtResult;
	JComboBox cmbJKType;
	private final ButtonGroup bgpMMHY = new ButtonGroup();
	public JXDatePicker byunjaeDatePicker = new JXDatePicker();
	SClient client;
	JRadioButton rbtOK;
	JRadioButton rbtNO;
	boolean dataReadFromServer = false;
	NumberFormatter numberFormatter;
	JButton btnOK;
	ArrayList<Component> allComponents;
        
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		//기본 폰트 설정
				try {
					UIDefaults swingComponentDefaultTable = UIManager.getDefaults();
					Enumeration<Object> allDefaultKey = swingComponentDefaultTable.keys();
					while(allDefaultKey.hasMoreElements()) {
						String defaultKey = allDefaultKey.nextElement().toString();
						if(defaultKey.indexOf("font") != -1) {
							Font newDefaultFont = new Font("맑은 고딕", Font.PLAIN, 12);
							//System.out.println(defaultKey);
							UIManager.put(defaultKey, newDefaultFont);
						}
					}
				UIManager.getDefaults().put("Button.margin", new Insets(0,0,0,0));
				} catch (Exception e) {
					e.printStackTrace();
				}	
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					A2003 frame = new A2003();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	/**
	 * Create the frame.
	 */
	public A2003() {
		MaskFormatter mf1 = null;
		try {
			mf1 = new MaskFormatter("###-##-######");
		} catch (ParseException e1) {
			e1.printStackTrace();
		}
		
		numberFormatter = new NumberFormatter( 
	            NumberFormat.getIntegerInstance());
		numberFormatter.setAllowsInvalid(false);  
	    numberFormatter.setCommitsOnValidEdit(true);
	    
		client = new SClient(null, 0, false);
		
		client.setListener(new SClient.OnListener() {
			@Override
			public void onRcvData(ByteBuffer rcvBuff) {
				try {
					byte[] trid = new byte[6];
					System.out.println("pos : "+ rcvBuff.position());
					rcvBuff.get(trid, 0, trid.length);
					String trString = new String(trid, "UTF-8");
					
					if(trString.equals("2222OO"))
					{
						System.out.println("2222OO");
						byte[] bytes = new byte[rcvBuff.remaining()];
						rcvBuff.get(bytes);
						String rString = new String(bytes, "UTF-8");
						txtAccName.setText(rString.substring(0,20).trim());
					}
					else if(trString.equals("2222XX"))
					{
						System.out.println("2222XX");
						txtResult.setText("해당 계좌가 존재하지 않습니다");
						setEmpty(allComponents);
						txtAccName.setText("");
						txtPassword.setText("");
					}
					else if(trString.equals("2222PX"))
					{
						System.out.println("2222PX");
						txtResult.setText("비밀번호가 일치하지 않습니다");
					}
					else if(trString.equals("2222OW"))
					{
						System.out.println("2222OW");
						requestJKList();
					}
					else if(trString.equals("20032X"))
					{
						System.out.println("20032X");
						txtResult.setText("등록이 정상처리 되지 않았습니다");
					}
					else if(trString.equals("20032R"))
					{
						System.out.println("20032R");
						txtResult.setText("질권 등록이 완료되었습니다");
					}
					else if(trString.equals("20033R"))
					{
						System.out.println("20033R");
						txtResult.setText("질권 내역이 변경되었습니다");
					}
					else if(trString.equals("20034R"))
					{
						System.out.println("20034R");
						txtResult.setText("질권이 해지되었습니다");
					}
					else if(trString.equals("20030X"))
					{
						System.out.println("20030X");
						if(cmbJKType.getSelectedIndex()==1)
						{
							txtResult.setText("질권설정 정보를 입력하여 주십시오");
							btnOK.setEnabled(true);
						}
						else
						{
							txtResult.setText("질권 내역이 존재하지 않습니다");
							btnOK.setEnabled(false);
						}
					}
					else if(trString.equals("20030R"))
					{
						if(cmbJKType.getSelectedIndex()==1)
						{
							btnOK.setEnabled(false);
						}
						else if(cmbJKType.getSelectedIndex()==2 || cmbJKType.getSelectedIndex()==3)
						{
							btnOK.setEnabled(true);
						}
						System.out.println("OR");
						txtResult.setText("질권 내역 조회가 완료되었습니다");
//						sprintf(msg.buf, "20030R%-20s%11s%4s%4s%-20s%-20s%4s%-20s%13s%-30s%-60s%-20s", 
//								jk_date, unit_acc, jk_id, jk_check, jk_amount, jk_byunjae, mmhy_check, 
//								jk_name, jk_ssn, jk_phone, jk_address, jk_etc);

						byte[] readByte = new byte[20];
						rcvBuff.get(readByte, 0, 19);
						//System.out.println("jk_date: "+new String(readByte, "UTF-8").trim());
						
						byte[] readByte2 = new byte[11];
						rcvBuff.get(readByte2, 0, 11);
						//System.out.println("unit_acc: "+new String(readByte2, "UTF-8"));
						
						byte[] readByte3 = new byte[4];
						rcvBuff.get(readByte3, 0, 4);
						//System.out.println("jk_id: "+new String(readByte3, "UTF-8"));
						
						byte[] readByte4 = new byte[4];
						rcvBuff.get(readByte4, 0, 4);
						//System.out.println("jk_check: "+new String(readByte4, "UTF-8"));
						
						byte[] readByte5 = new byte[20];
						rcvBuff.get(readByte5, 0, 20);
						//System.out.println("jk_amount: "+new String(readByte5, "UTF-8").trim());
						txtJKAmount.setValue(Integer.parseInt(new String(readByte5, "UTF-8").trim()));
						
						byte[] readByte6 = new byte[20];
						rcvBuff.get(readByte6, 0, 20);
						//System.out.println("bj_date: "+new String(readByte6, "UTF-8").trim());
						
						dataReadFromServer = true;
						//YY/MM/DD를 yyyy/MM/dd로 변경
						SimpleDateFormat dfFromDB
						= new SimpleDateFormat("yy/MM/dd", Locale.KOREA);
						//System.out.println(dfFromDB.format(new Date()).toString());
						String bj_date_String = new String(readByte6, "UTF-8");
						Date bjdate = null;
						try {
							bjdate = 
							dfFromDB.parse(bj_date_String);
						} catch (ParseException e) {
							//e.printStackTrace();
							System.out.println("date data parsing err");
						}
						byunjaeDatePicker.setDate(bjdate);
						dataReadFromServer = false;
						
						byte[] readByte7 = new byte[4];
						rcvBuff.get(readByte7, 0, 4);
						String mm_check = new String(readByte7, "UTF-8");
						//System.out.println("mm_check: "+ mm_check);
						if(mm_check.equals("OOOO"))
						{
							rbtOK.setSelected(true);
						}
						else
						{
							rbtNO.setSelected(true);
						}
						
						byte[] readByte8 = new byte[20];
						rcvBuff.get(readByte8, 0, 19);
						//System.out.println("jk_name: "+new String(readByte8, "UTF-8").trim());
						txtJKName.setText(new String(readByte8, "UTF-8").trim());
						
						byte[] readByte9 = new byte[13];
						rcvBuff.get(readByte9, 0, 13);
						//System.out.println("jk_ssn: "+new String(readByte9, "UTF-8"));
						txtJKSSN.setText(new String(readByte9, "UTF-8"));
						
						byte[] readByte10 = new byte[30];
						rcvBuff.get(readByte10, 0, 30);
						//System.out.println("jk_phone: "+new String(readByte10, "UTF-8").trim());
						String JKPhoneString = new String(readByte10, "UTF-8").trim();
						if(JKPhoneString.length()<=3)
							txtJKPhone1.setText(JKPhoneString);
						else
						{
							txtJKPhone1.setText(JKPhoneString.substring(0,3));
							if(JKPhoneString.length()<=7)
								txtJKPhone2.setText(JKPhoneString.substring(3));
							else
							{
								txtJKPhone2.setText(JKPhoneString.substring(3,7));
								txtJKPhone3.setText(JKPhoneString.substring(7));
							}
						}
						
						byte[] readByte11 = new byte[60];
						rcvBuff.get(readByte11, 0, 60);
						//System.out.println("jk_addr: "+new String(readByte11, "UTF-8").trim());
						txtJKAddress.setText(new String(readByte11, "UTF-8").trim());
						
						byte[] readByte12 = new byte[20];
						rcvBuff.get(readByte12, 0, 20);
						//System.out.println("jk_etc: "+new String(readByte12, "UTF-8").trim());
						txtJKetc.setText(new String(readByte12, "UTF-8").trim());
						//System.out.println("buff pos : " + rcvBuff.position());
						//System.out.println("buff lim : " + rcvBuff.limit());
						
					}
					
				} catch (UnsupportedEncodingException e1) {
				e1.printStackTrace();
				}
			}
		});
		setTitle("CoreBASE [2003] 계좌질권설정");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 669, 430);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("처리구분");
		lblNewLabel.setBounds(12, 10, 89, 15);
		contentPane.add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("계좌번호");
		lblNewLabel_1.setBounds(12, 35, 57, 15);
		contentPane.add(lblNewLabel_1);
		
		txtAccNum = new JFormattedTextField(mf1);
		txtAccNum.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if(e.getKeyCode()==KeyEvent.VK_ENTER)
					findAccountName();
			}
		});
		txtAccNum.setDocument(new JTextFieldLimit(13));
		txtAccNum.setBounds(113, 32, 116, 21);
		contentPane.add(txtAccNum);
		txtAccNum.setColumns(10);
		
		txtAccName = new JTextField();
		txtAccName.setFocusTraversalKeysEnabled(false);
		txtAccName.setFocusable(false);
		txtAccName.setEditable(false);
		txtAccName.setBounds(241, 32, 116, 21);
		contentPane.add(txtAccName);
		txtAccName.setColumns(10);
		
		JLabel lblNewLabel_2 = new JLabel("비밀번호");
		lblNewLabel_2.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNewLabel_2.setBounds(369, 35, 57, 15);
		contentPane.add(lblNewLabel_2);
		
		btnOK = new JButton("확인");
		btnOK.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				/**
				 * 확인 버튼
				 */
				ByteBuffer sendBuffer = ByteBuffer.allocateDirect(1024);
				sendBuffer.clear();
				switch(cmbJKType.getSelectedIndex())
				{
				case 0:
					System.out.println("case 0");
					break;
					// do nothing.
				case 1:
					System.out.println("case 1 - 등록");
					
					//tr		06
					sendBuffer.position(0);
					sendBuffer.limit(6);
					String a1 = "20032S";
					sendBuffer.put(a1.getBytes());
					
					//계좌		11	6~17
					sendBuffer.position(sendBuffer.limit());
					sendBuffer.limit(sendBuffer.limit()+11);
					sendBuffer.put(txtAccNum.getText().replaceAll("-", "").getBytes());
					
					//등록구분	04	1111, 2222, 3333  등록, 정정, 해지.	
					sendBuffer.position(sendBuffer.limit());
					sendBuffer.limit(sendBuffer.limit()+4);
					sendBuffer.put("1111".getBytes());
					
					//금액		20
					sendBuffer.position(sendBuffer.limit());
					sendBuffer.limit(sendBuffer.limit()+20);
					sendBuffer.put(txtJKAmount.getText().replaceAll(",","").getBytes());
					
					//변제날짜	20
					SimpleDateFormat dftoDB = new SimpleDateFormat("yyyy/MM/dd", Locale.KOREA);
					String aa = dftoDB.format(byunjaeDatePicker.getDate());
					sendBuffer.position(sendBuffer.limit());
					sendBuffer.limit(sendBuffer.limit()+20);
					sendBuffer.put(aa.getBytes());
					
//					//변제허용	4	OOOO, XXXX
					sendBuffer.position(sendBuffer.limit());
					sendBuffer.limit(sendBuffer.limit()+4);
					if(rbtOK.isSelected())
					{
						sendBuffer.put("OOOO".getBytes());
					}
					else if(rbtNO.isSelected())
					{
						sendBuffer.put("XXXX".getBytes());
					}
					
					//법인명
					sendBuffer.position(sendBuffer.limit());
					sendBuffer.limit(sendBuffer.limit()+20);
					sendBuffer.put(txtJKName.getText().getBytes());
					
					//실명확인번호
					sendBuffer.position(sendBuffer.limit());
					sendBuffer.limit(sendBuffer.limit()+13);
					sendBuffer.put(txtJKSSN.getText().getBytes());
					
					//전화번호
					sendBuffer.position(sendBuffer.limit());
					sendBuffer.limit(sendBuffer.limit()+30);
					String phone = txtJKPhone1.getText()+txtJKPhone2.getText()+txtJKPhone3.getText();
					sendBuffer.put(phone.getBytes());
					
					//주소
					sendBuffer.position(sendBuffer.limit());
					sendBuffer.limit(sendBuffer.limit()+60);
					sendBuffer.put(txtJKAddress.getText().getBytes());
					
					//기타
					sendBuffer.position(sendBuffer.limit());
					sendBuffer.limit(sendBuffer.limit()+20);
					sendBuffer.put(txtJKetc.getText().getBytes());
					
//					sendBuffer.position(sendBuffer.limit());
//					sendBuffer.limit(sendBuffer.limit()+1);
//					sendBuffer.put("$".getBytes());
					
					sendBuffer.limit(1024);
					sendBuffer.rewind();
					client.writeBuffer(sendBuffer);
					sendBuffer.clear();
					break;
				case 2:
					System.out.println("case 2 - 정정");
					sendBuffer.position(0);
					sendBuffer.limit(6);
					sendBuffer.put("20033S".getBytes());
					
					//계좌		11	6~17
					sendBuffer.position(sendBuffer.limit());
					sendBuffer.limit(sendBuffer.limit()+11);
					sendBuffer.put(txtAccNum.getText().replaceAll("-", "").getBytes());
					
					//등록구분	04	1111, 2222, 3333  등록, 정정, 해지.	
					sendBuffer.position(sendBuffer.limit());
					sendBuffer.limit(sendBuffer.limit()+4);
					sendBuffer.put("2222".getBytes());
					
					//금액		20
					sendBuffer.position(sendBuffer.limit());
					sendBuffer.limit(sendBuffer.limit()+20);
					sendBuffer.put(txtJKAmount.getText().replaceAll(",","").getBytes());
					
					//변제날짜	20
					dftoDB = new SimpleDateFormat("yyyy/MM/dd", Locale.KOREA);
					aa = dftoDB.format(byunjaeDatePicker.getDate());
					sendBuffer.position(sendBuffer.limit());
					sendBuffer.limit(sendBuffer.limit()+20);
					sendBuffer.put(aa.getBytes());
					
//					//변제허용	4	OOOO, XXXX
					sendBuffer.position(sendBuffer.limit());
					sendBuffer.limit(sendBuffer.limit()+4);
					if(rbtOK.isSelected())
					{
						sendBuffer.put("OOOO".getBytes());
					}
					else if(rbtNO.isSelected())
					{
						sendBuffer.put("XXXX".getBytes());
					}
					
					//법인명
					sendBuffer.position(sendBuffer.limit());
					sendBuffer.limit(sendBuffer.limit()+20);
					sendBuffer.put(txtJKName.getText().getBytes());
					
					//실명확인번호
					sendBuffer.position(sendBuffer.limit());
					sendBuffer.limit(sendBuffer.limit()+13);
					sendBuffer.put(txtJKSSN.getText().getBytes());
					
					//전화번호
					sendBuffer.position(sendBuffer.limit());
					sendBuffer.limit(sendBuffer.limit()+30);
					sendBuffer.put((txtJKPhone1.getText()+txtJKPhone2.getText()+txtJKPhone3.getText()).getBytes());
					
					//주소
					sendBuffer.position(sendBuffer.limit());
					sendBuffer.limit(sendBuffer.limit()+60);
					sendBuffer.put(txtJKAddress.getText().getBytes());
					
					//기타
					sendBuffer.position(sendBuffer.limit());
					sendBuffer.limit(sendBuffer.limit()+20);
					sendBuffer.put(txtJKetc.getText().getBytes());
					
//					sendBuffer.position(sendBuffer.limit());
//					sendBuffer.limit(sendBuffer.limit()+1);
//					sendBuffer.put("$".getBytes());
					
					sendBuffer.limit(1024);
					sendBuffer.rewind();
					client.writeBuffer(sendBuffer);
					sendBuffer.clear();
					break;
				case 3:
					System.out.println("case 3 - 해지");
					sendBuffer.position(0);
					sendBuffer.limit(6);
					sendBuffer.put("20034S".getBytes());
					
					//계좌		11	6~17
					sendBuffer.position(sendBuffer.limit());
					sendBuffer.limit(sendBuffer.limit()+11);
					sendBuffer.put(txtAccNum.getText().replaceAll("-", "").getBytes());
					
					//등록구분	04	1111, 2222, 3333  등록, 정정, 해지.	
					sendBuffer.position(sendBuffer.limit());
					sendBuffer.limit(sendBuffer.limit()+4);
					sendBuffer.put("3333".getBytes());
					
					//금액		20
					sendBuffer.position(sendBuffer.limit());
					sendBuffer.limit(sendBuffer.limit()+20);
					sendBuffer.put(txtJKAmount.getText().replaceAll(",","").getBytes());
					
					//변제날짜	20
					dftoDB = new SimpleDateFormat("yyyy/MM/dd", Locale.KOREA);
					aa = dftoDB.format(byunjaeDatePicker.getDate());
					sendBuffer.position(sendBuffer.limit());
					sendBuffer.limit(sendBuffer.limit()+20);
					sendBuffer.put(aa.getBytes());
					
//					//변제허용	4	OOOO, XXXX
					sendBuffer.position(sendBuffer.limit());
					sendBuffer.limit(sendBuffer.limit()+4);
					if(rbtOK.isSelected())
					{
						sendBuffer.put("OOOO".getBytes());
					}
					else if(rbtNO.isSelected())
					{
						sendBuffer.put("XXXX".getBytes());
					}
					
					//법인명
					sendBuffer.position(sendBuffer.limit());
					sendBuffer.limit(sendBuffer.limit()+20);
					sendBuffer.put(txtJKName.getText().getBytes());
					
					//실명확인번호
					sendBuffer.position(sendBuffer.limit());
					sendBuffer.limit(sendBuffer.limit()+13);
					sendBuffer.put(txtJKSSN.getText().getBytes());
					
					//전화번호
					sendBuffer.position(sendBuffer.limit());
					sendBuffer.limit(sendBuffer.limit()+30);
					sendBuffer.put((txtJKPhone1.getText()+txtJKPhone2.getText()+txtJKPhone3.getText()).getBytes());
					
					//주소
					sendBuffer.position(sendBuffer.limit());
					sendBuffer.limit(sendBuffer.limit()+60);
					sendBuffer.put(txtJKAddress.getText().getBytes());
					
					//기타
					sendBuffer.position(sendBuffer.limit());
					sendBuffer.limit(sendBuffer.limit()+20);
					sendBuffer.put(txtJKetc.getText().getBytes());
					
//					sendBuffer.position(sendBuffer.limit());
//					sendBuffer.limit(sendBuffer.limit()+1);
//					sendBuffer.put("$".getBytes());
					
					sendBuffer.limit(1024);
					sendBuffer.rewind();
					client.writeBuffer(sendBuffer);
					sendBuffer.clear();
					break;
				}//switch
				if(cmbJKType.getSelectedIndex()!=1)
					setEmpty(allComponents);
				btnOK.setEnabled(false);
			}//event-handler
		});
		btnOK.setEnabled(false);
		btnOK.setBounds(507, 31, 65, 23);
		contentPane.add(btnOK);
		
		JButton btnSend = new JButton("전송");
		btnSend.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				/**
				 * 전송 버튼
				 */
				getJKListByAccPW();
			}
		});
		btnSend.setBounds(584, 31, 65, 23);
		contentPane.add(btnSend);
		
		JSeparator separator = new JSeparator();
		separator.setBounds(12, 60, 637, 2);
		contentPane.add(separator);
		
		JLabel lblNewLabel_3 = new JLabel("질권설정정보");
		lblNewLabel_3.setBounds(12, 72, 89, 15);
		contentPane.add(lblNewLabel_3);
		
		JSeparator separator_1 = new JSeparator();
		separator_1.setBounds(12, 97, 637, 2);
		contentPane.add(separator_1);
		
		JLabel lblNewLabel_4 = new JLabel("질권금액");
		lblNewLabel_4.setBounds(12, 109, 89, 15);
		contentPane.add(lblNewLabel_4);
		
		JLabel lblNewLabel_5 = new JLabel("변제기일");
		lblNewLabel_5.setBounds(12, 134, 89, 15);
		contentPane.add(lblNewLabel_5);
		
		JLabel lblNewLabel_6 = new JLabel("매매허용여부");
		lblNewLabel_6.setBounds(12, 159, 90, 15);
		contentPane.add(lblNewLabel_6);
		
		JSeparator separator_2 = new JSeparator();
		separator_2.setBounds(12, 184, 637, 2);
		contentPane.add(separator_2);
		
		JLabel lblNewLabel_7 = new JLabel("질권설정내역");
		lblNewLabel_7.setBounds(12, 196, 89, 15);
		contentPane.add(lblNewLabel_7);
		
		JSeparator separator_3 = new JSeparator();
		separator_3.setBounds(12, 221, 637, 2);
		contentPane.add(separator_3);
		
		JLabel lblNewLabel_8 = new JLabel("법인명");
		lblNewLabel_8.setBounds(12, 233, 89, 15);
		contentPane.add(lblNewLabel_8);
		
		JLabel lblNewLabel_9 = new JLabel("실명확인번호");
		lblNewLabel_9.setBounds(12, 258, 89, 15);
		contentPane.add(lblNewLabel_9);
		
		JLabel lblNewLabel_10 = new JLabel("전화번호");
		lblNewLabel_10.setBounds(12, 283, 89, 15);
		contentPane.add(lblNewLabel_10);
		
		JLabel lblNewLabel_11 = new JLabel("주소");
		lblNewLabel_11.setBounds(12, 308, 89, 15);
		contentPane.add(lblNewLabel_11);
		
		JLabel lblNewLabel_12 = new JLabel("비고");
		lblNewLabel_12.setBounds(12, 333, 89, 15);
		contentPane.add(lblNewLabel_12);
		
		txtJKAmount = new JFormattedTextField(numberFormatter);
		txtJKAmount.setBounds(113, 106, 116, 21);
		contentPane.add(txtJKAmount);
		txtJKAmount.setColumns(10);
		
		rbtOK = new JRadioButton("허용");
		bgpMMHY.add(rbtOK);
		rbtOK.setBounds(113, 155, 57, 23);
		contentPane.add(rbtOK);
		
		rbtNO = new JRadioButton("불가");
		bgpMMHY.add(rbtNO);
		rbtNO.setBounds(174, 155, 57, 23);
		contentPane.add(rbtNO);
		
		txtJKName = new JTextField();
		txtJKName.setBounds(113, 230, 116, 21);
		contentPane.add(txtJKName);
		txtJKName.setColumns(10);
		
		txtJKSSN = new JTextField();
		txtJKSSN.setDocument(new JTextFieldLimit(13));
		txtJKSSN.setBounds(113, 255, 116, 21);
		contentPane.add(txtJKSSN);
		txtJKSSN.setColumns(10);
		
		txtJKPhone1 = new JTextField();
		txtJKPhone1.setBounds(113, 280, 57, 21);
		contentPane.add(txtJKPhone1);
		txtJKPhone1.setColumns(10);
		
		txtJKAddress = new JTextField();
		txtJKAddress.setBounds(113, 305, 536, 21);
		contentPane.add(txtJKAddress);
		txtJKAddress.setColumns(10);
		
		txtJKetc = new JTextField();
		txtJKetc.setBounds(113, 330, 536, 21);
		contentPane.add(txtJKetc);
		txtJKetc.setColumns(10);
		
		txtJKPhone2 = new JTextField();
		txtJKPhone2.setBounds(178, 280, 57, 21);
		contentPane.add(txtJKPhone2);
		txtJKPhone2.setColumns(10);
		
		txtJKPhone3 = new JTextField();
		txtJKPhone3.setBounds(243, 280, 57, 21);
		contentPane.add(txtJKPhone3);
		txtJKPhone3.setColumns(10);
		
		JLabel label = new JLabel("-");
		label.setBounds(170, 283, 6, 15);
		contentPane.add(label);
		
		JLabel label_1 = new JLabel("-");
		label_1.setBounds(236, 283, 6, 15);
		contentPane.add(label_1);
		
		txtPassword = new JPasswordField();
		txtPassword.setDocument(new JTextFieldLimit(4));
		txtPassword.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				if(e.getKeyChar() == KeyEvent.VK_ENTER)
				{
					getJKListByAccPW();
				}
			}
		});
		txtPassword.setDocument(new JTextFieldLimit(4));
		txtPassword.setEchoChar('*');
		txtPassword.setBounds(438, 32, 60, 21);
		contentPane.add(txtPassword);
		
		JLabel lblNewLabel_14 = new JLabel("<< 질권설정 가능 상품은 위탁/선물옵션/수익증권 계좌만 가능합니다.");
		lblNewLabel_14.setBounds(260, 109, 389, 15);
		contentPane.add(lblNewLabel_14);
		
		JLabel lblNewLabel_15 = new JLabel("<< 등록시 설정정보/설정내역 전 항목을 입력해야 합니다.");
		lblNewLabel_15.setBounds(260, 124, 389, 15);
		contentPane.add(lblNewLabel_15);
		
		JLabel lblNewLabel_16 = new JLabel("<< 정정시 매매허용여부는 변경 불가합니다.");
		lblNewLabel_16.setBounds(260, 138, 389, 15);
		contentPane.add(lblNewLabel_16);
		
		txtResult = new JTextField();
		txtResult.setFont(new Font("맑은 고딕", Font.BOLD, 12));
		txtResult.setForeground(Color.RED);
		txtResult.setEditable(false);
		txtResult.setBounds(12, 365, 637, 21);
		contentPane.add(txtResult);
		txtResult.setColumns(10);
		
		// datepicker
		byunjaeDatePicker.setDate(new Date());
		PropertyChangeListener listener = new PropertyChangeListener() {
			public void propertyChange(PropertyChangeEvent e) {
				if ("date".equals(e.getPropertyName())) {
					Date selectedDate = byunjaeDatePicker.getDate();
					Date today = new Date();
					if(selectedDate==null || today==null) ;
					else if(!dataReadFromServer && selectedDate.before(today))
					{
						byunjaeDatePicker.setDate(today);
						txtResult.setText("변제기일은 오늘 이후만 가능합니다");
					}
				}
			}
		};
		byunjaeDatePicker.addPropertyChangeListener(listener);
		
		byunjaeDatePicker.getEditor().setEditable(false);
		ImageIcon lg = new ImageIcon("img\\cal2.png");//endar.PNG");
		Image lgImage = lg.getImage();  
		ImageIcon loginImage = new ImageIcon(lgImage.getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH));
		
		JButton btnDateTo = (JButton)byunjaeDatePicker.getComponent(1);
		btnDateTo.setIcon(loginImage);
		btnDateTo.setBorder(BorderFactory.createEmptyBorder());
		
		byunjaeDatePicker.setFormats(new SimpleDateFormat("yyyy/MM/dd", Locale.KOREA));		
		byunjaeDatePicker.setBounds(113, 131, 116, 21);
		contentPane.add(byunjaeDatePicker);
		
		//JFormattedTextField readTimeOut = new JFormattedTextField(numberFormatter);
		//readTimeOut.setColumns(20);
	    //readTimeOut.setHorizontalAlignment(SwingConstants.RIGHT);
	    //datePicker
		
		allComponents = new ArrayList<Component>();
		
		allComponents.add(txtJKName);
		allComponents.add(txtJKSSN);
		allComponents.add(txtJKPhone1);
		allComponents.add(txtJKPhone2);
		allComponents.add(txtJKPhone3);
		allComponents.add(txtJKAddress);
		allComponents.add(txtJKetc);
		
		cmbJKType = new JComboBox();
		cmbJKType.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnOK.setEnabled(false);
				setEmpty(allComponents);
			}
		});
		cmbJKType.setFocusable(false);
		cmbJKType.setModel(new DefaultComboBoxModel(new String[] {"조회", "등록", "정정", "해지"}));
		cmbJKType.setSelectedIndex(0);
		cmbJKType.setBounds(113, 6, 90, 23);
		contentPane.add(cmbJKType);
	}
	/**
	 * 계좌명 조회 전문 전송
	 */
	public void findAccountName()
	{
		if(txtAccNum.getText().length()==13)
		{
			if(txtAccNum.getText().substring(4,6).equals("01") || txtAccNum.getText().substring(4,6).equals("51"))
			{
				String accountFindString = "2222ID";
				accountFindString+=String.format("%11s", txtAccNum.getText().replaceAll("-", ""));
				client.writeString(accountFindString);//+"$");
				System.out.println(accountFindString);
				//System.out.println("계좌조회"+accountFindString);
				txtResult.setText("계좌조회");
			}
			else
			{
				txtResult.setText("질권설정은 개별계좌(01, 51)만 가능합니다.");
			}
		}
		else
		{
			txtResult.setText("계좌번호 11자리를 입력해 주세요.");	
		}
	}
	
	/**
	 * 계좌 질권설정 내역 조회 전문 전송
	 */
	public void getJKListByAccPW()
	{
		if(txtPassword.getPassword().length!=4)
		{
			if(txtAccNum.getText().length()!=11)
			{
				txtResult.setText("계좌번호11자리, 비밀번호4자리를 입력하여 주십시오.");
			}
			else
			{
				txtResult.setText("비밀번호 4자리를 바르게 입력하여 주십시오.");
			}
		}
		else
		{
			//System.out.println("질권 설정 내역 조회");
			// 질권 설정 내역 조회.
			String accountFindString = "2222IW";
			accountFindString+=String.format("%11s%4s", txtAccNum.getText().replaceAll("-", ""), new String(txtPassword.getPassword()));
			client.writeString(accountFindString);//+"$");
			//System.out.println(accountFindString);
			txtResult.setText("");
		}
	}
	
	/**
	 * 모든칸 지우기
	 */
	
	public void setEmpty(ArrayList<Component> c)
	{
		for(Component d : c)
		{
			JTextField e = (JTextField)d;
			e.setText("");
		}
		txtJKAmount.setValue(null);
		byunjaeDatePicker.setDate(null);
		bgpMMHY.clearSelection();
	}
	
	/**
	 * 모든칸 editable ON/OFF
	 */
	public void setEditable(ArrayList<Component> c, boolean boool)
	{
		for(Component d : c)
		{
			JTextField e = (JTextField)d;
			e.setEditable(boool);
		}
	}
	
	public void requestJKList()
	{
		String jkFindString = "20030S";
		jkFindString+=String.format("%11s%4s", txtAccNum.getText().replaceAll("-", ""), new String(txtPassword.getPassword()));
		client.writeString(jkFindString);//+"$");
		setEmpty(allComponents);
	}
}
